package org.swift.mashup.engine.concurrency.memory;

import java.util.Collection;
import java.util.Collections;

import org.swift.commons.xml.XmlUtils;
import org.w3c.dom.Node;

public class NumericMemoryItem extends MemoryItem {

	protected final Number value;
	
	public NumericMemoryItem(int id, Number value, MemoryType type) {
		super(id, type);
		this.value = value;
	}

	@Override
	public Object getValue() {
		return value;
	}

	@Override
	public Collection<Number> toCollection() {
		return Collections.singletonList(value);
	}

	@Override
	public Node toNode() {
		String xml = toXML();		
		try {
			return XmlUtils.toDocument(xml);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public String toXML() {
		String str = toString();
		
		if (XmlUtils.isXml(str)) {
			return str;
		} else {
			return "<" + this.getType().getName() + ">" 
				+ str 
				+ "</" + this.getType().getName() + ">";
		}		
	}

	@Override
	public String toString() {
		return String.valueOf(value);
	}
}
